#include "MyMessageQueue.h"

#include <sstream>
#include <thread>

#include "utils/Logger.h"
namespace test_bridge {

void MyMessageQueue::runOnQueue(std::function<void()>&& func) {
  std::lock_guard<std::mutex> lock(mutex_);
  std::stringstream ss;
  ss << "producer thread [" << std::this_thread::get_id() << "] submitted msg to " << queueName_ << std::endl;
  logInfo(ss.str());
  queue_.push(func);
  cond_.notify_all();
}

void MyMessageQueue::execute() {
  std::unique_lock<std::mutex> lock(mutex_);
  cond_.wait(lock, [this] { return !queue_.empty(); });
  std::stringstream ss;
  ss << "consumer thread [" << std::this_thread::get_id() << "] is consuming a msg from " << queueName_ << std::endl;
  logInfo(ss.str());
  auto func = queue_.front();
  func();
  queue_.pop();
}
}  // namespace test_bridge